package com.naiterui.ehp.bs.esign.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.bs.esign.api.bjca.vo.SignFileUploadVO;
import com.naiterui.ehp.bs.esign.api.vo.SignFileSnVO;
import com.naiterui.ehp.bs.esign.entity.mysql.EsignNoSecret;
import com.naiterui.ehp.bs.esign.repository.mysql.IEsignNoSecretRepository;
import com.naiterui.ehp.bs.esign.service.INoSecretService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 免密签名
 *
 * @author yangfuya
 * @version 1.0
 * @date 2021/4/28 18:16
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class INoSecretServiceImpl implements INoSecretService {

    private final IEsignNoSecretRepository esignNoSecretRepository;
    @Value("${naiterui.scca-risk-warning-notice}")
    private String informPath;

    /**
     * 保存免签记录
     *
     * @param signUserId
     * @param signUserType
     * @return
     */
    @Override
    public EsignNoSecret saveEsignNoSecret(Long signUserId, Integer signUserType) {
        EsignNoSecret esignNoSecret = esignNoSecretRepository.save(EsignNoSecret.builder()
                .signUserId(signUserId)
                .signUserType(signUserType)
                .informName("免密风险提示告知书")
                .informUrl(informPath)
                .status(0)
                .createdAt(new Date())
                .build());
        return esignNoSecret;
    }

    /**
     * 获取签名文件信息
     *
     * @param userId       签名用户id
     * @param bizSn        业务编码
     * @param pdfPath      签名文件地址
     * @param signUserType 签名用户类型
     * @param x            x坐标
     * @param y            y坐标
     * @param createAt     单据创建时间
     * @return
     */
    @Override
    public SignFileUploadVO getSignFileUploadVO(Long userId, String bizSn, String pdfPath, String signUserType, Float x, Float y, Date createAt) {
        return SignFileUploadVO.builder()
                .signUserId(userId)
                .signUserType(signUserType)
                .signFileType(SignFileSnVO.FILE_TYPE_NS)
                .pdfPath(pdfPath)
                .bizSn(bizSn)
                .patientName("")
                .patientGender(0)
                .patientAge("")
                .keyword("")
                .unionId("")
                .stampX(x)
                .stampY(y)
                .createdAt(createAt)
                .build();
    }

    @Override
    public String getNoSecretBizSn(Long signUserId, Integer signUserType) {
        EsignNoSecret esignNoSecret = esignNoSecretRepository
            .findFirstBySignUserIdAndSignUserTypeAndStatusOrderByCreatedAtDesc(signUserId, signUserType, 0);
        if (null == esignNoSecret) {
            log.info("getNoSecretBizSn is null signUserId: {}, signUserType: {}", signUserId, signUserType);
            return "0";
        }
        return esignNoSecret.getId().toString();
    }
}
